/*
 * HAPSIEEditaArea.java
 *
 * Created on 24/01/2012, 06:27:52 PM
 */
package interfaz.gestionAreas;

import java.util.List;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import org.hibernate.Transaction;
import pojos.Areas;
import usoHAPSIE.HAPSIEApp;

/**
 *
 * @author Erika Berenice Hernandez Angulo
 */
public class EditaArea extends javax.swing.JDialog {
    
    List listAreas;  
    JDialog gestionDeAreas;
    public Areas nueva; 
    
    /** Creates new form HAPSIEEditaArea */
    public EditaArea(java.awt.Dialog parent) {
        super(parent);
        initComponents(); 
    }

    public EditaArea(java.awt.Dialog parent ,  int indice, List listAreas){
        super(parent);
        initComponents();
        this.listAreas = listAreas;
        nueva = (Areas) listAreas.get(indice);
        muestraA();
    }
    
    private void muestraA(){
       textEditaArea.setText(nueva.getNombreArea());
       textEditaEncargado.setText(nueva.getEncargado());         
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelIntroArea = new javax.swing.JLabel();
        textEditaArea = new javax.swing.JTextField();
        labelIntroEncargado = new javax.swing.JLabel();
        textEditaEncargado = new javax.swing.JTextField();
        botonAceptaEditar = new javax.swing.JButton();
        botonCancelaEditar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        etiquetaCampoOpcional = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(EditaArea.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        labelIntroArea.setFont(resourceMap.getFont("labelIntroArea.font")); // NOI18N
        labelIntroArea.setText(resourceMap.getString("labelIntroArea.text")); // NOI18N
        labelIntroArea.setName("labelIntroArea"); // NOI18N

        textEditaArea.setText(resourceMap.getString("textEditaArea.text")); // NOI18N
        textEditaArea.setName("textEditaArea"); // NOI18N

        labelIntroEncargado.setFont(resourceMap.getFont("labelIntroEncargado.font")); // NOI18N
        labelIntroEncargado.setText(resourceMap.getString("labelIntroEncargado.text")); // NOI18N
        labelIntroEncargado.setName("labelIntroEncargado"); // NOI18N

        textEditaEncargado.setText(resourceMap.getString("textEditaEncargado.text")); // NOI18N
        textEditaEncargado.setName("textEditaEncargado"); // NOI18N
        textEditaEncargado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textEditaEncargadoActionPerformed(evt);
            }
        });

        botonAceptaEditar.setFont(resourceMap.getFont("botonAceptaEditar.font")); // NOI18N
        botonAceptaEditar.setText(resourceMap.getString("botonAceptaEditar.text")); // NOI18N
        botonAceptaEditar.setName("botonAceptaEditar"); // NOI18N
        botonAceptaEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptaEditarActionPerformed(evt);
            }
        });

        botonCancelaEditar.setFont(resourceMap.getFont("botonCancelaEditar.font")); // NOI18N
        botonCancelaEditar.setText(resourceMap.getString("botonCancelaEditar.text")); // NOI18N
        botonCancelaEditar.setName("botonCancelaEditar"); // NOI18N
        botonCancelaEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelaEditarActionPerformed(evt);
            }
        });

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        etiquetaCampoOpcional.setText(resourceMap.getString("etiquetaCampoOpcional.text")); // NOI18N
        etiquetaCampoOpcional.setName("etiquetaCampoOpcional"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(botonAceptaEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                .addComponent(botonCancelaEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(380, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(29, 29, 29))
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelIntroEncargado, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelIntroArea, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(216, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textEditaArea, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                            .addComponent(textEditaEncargado, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                            .addComponent(etiquetaCampoOpcional))
                        .addGap(82, 82, 82))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(7, 7, 7)
                .addComponent(labelIntroArea)
                .addGap(29, 29, 29)
                .addComponent(textEditaArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(labelIntroEncargado)
                .addGap(18, 18, 18)
                .addComponent(textEditaEncargado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(etiquetaCampoOpcional)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCancelaEditar, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .addComponent(botonAceptaEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void botonAceptaEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptaEditarActionPerformed

    if(textEditaArea.getText().trim().equals("")){
        JOptionPane.showMessageDialog(this, "Los campos están vacios!!");
    } else {
        int response = JOptionPane.showConfirmDialog(this, "¿Desea guardar los cambios?");
        if(response == JOptionPane.YES_NO_OPTION){
        Transaction transaccion = HAPSIEApp.servicioConexion.session.beginTransaction();
        nueva.setNombreArea(textEditaArea.getText());
        nueva.setEncargado(textEditaEncargado.getText());
        HAPSIEApp.servicioConexion.session.update(nueva);
        transaccion.commit();
        ((GestionDeAreas) getOwner()).actualizaTabla();
        getOwner().setVisible(true);
        this.setVisible(false);
        }
    }
}//GEN-LAST:event_botonAceptaEditarActionPerformed

private void textEditaEncargadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textEditaEncargadoActionPerformed
}//GEN-LAST:event_textEditaEncargadoActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    int response = JOptionPane.showConfirmDialog(this, "¿Realmente desea salir?");
    if(response == JOptionPane.YES_OPTION){
        getOwner().setVisible(true);
        this.setVisible(false);  
    }
}//GEN-LAST:event_formWindowClosing

private void botonCancelaEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelaEditarActionPerformed
    int  response = JOptionPane.showConfirmDialog(this, "¿Realmente desea salir?");
    if(response == JOptionPane.YES_OPTION){
        getOwner().setVisible(true);
        this.setVisible(false);
    }
      
}//GEN-LAST:event_botonCancelaEditarActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptaEditar;
    private javax.swing.JButton botonCancelaEditar;
    private javax.swing.JLabel etiquetaCampoOpcional;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelIntroArea;
    private javax.swing.JLabel labelIntroEncargado;
    private javax.swing.JTextField textEditaArea;
    private javax.swing.JTextField textEditaEncargado;
    // End of variables declaration//GEN-END:variables
}
